package com.yazi.mxz.person.controller.shop;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.custom.GoodsReportView;
import com.yazi.mxz.common.entity.custom.Order4Search;
import com.yazi.mxz.common.entity.enums.OrderStatus;
import com.yazi.mxz.common.entity.enums.StoreGoodsStatus;
import com.yazi.mxz.common.service.GoodsReportViewService;
import com.yazi.mxz.common.service.OrderService;
import com.yazi.mxz.common.service.StoreGoodsService;

@Controller("shop-indexController")
@RequestMapping("/shop")
public class CustomIndexController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CustomIndexController.class);
	
	@Resource
	StoreGoodsService storeGoodsService;
	@Resource
	OrderService orderService;
	@Resource
	GoodsReportViewService goodsReportViewService;
	
	@RequestMapping
	public String index(Model model) {
		debug(log, "进入店铺管理首页");
		
		// 今天
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long startTime = cal.getTimeInMillis();
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long endTime = cal.getTimeInMillis();
		
		// 新订单数(线上)
		Order4Search order = new Order4Search();
		order.setStore(getStore());
		order.setStatus(OrderStatus.NEW.ordinal());
		int newCount = orderService.count(order, null);
		
		
		// 获取店铺上架商品数
		StoreGoods storeGoods = new StoreGoods();
		storeGoods.setStore(getStore());
		storeGoods.setStatus(StoreGoodsStatus.ON.ordinal());
		int sgCount = storeGoodsService.count(storeGoods, null);// storeGoods count of selling
		
		// 今日线上订单数
		order = new Order4Search();
		order.setStore(getStore());
		order.setIsLocalSale(false);
		order.setStartTime(startTime);
		order.setEndTime(endTime);
		int onCount = orderService.count(order, null);
		
		// 今日线下订单数
		order.setIsLocalSale(true);
		int offCount = orderService.count(order, null);
		
		// 今日销售额
		List<GoodsReportView> list = goodsReportViewService.sumByStoreId(getStoreId(), startTime, endTime);
		Float revenue = null;
		if (null != list && list.size() > 0) {
			revenue = list.get(0).getRevenue();
		}
		if (null == revenue) {
			revenue = 0f;
		}
		
		model.addAttribute("newCount", newCount);
		model.addAttribute("sgCount", sgCount);
		model.addAttribute("onCount", onCount);
		model.addAttribute("offCount", offCount);
		model.addAttribute("revenue", revenue);
		model.addAttribute("store", getStore());
		return "/shop/index";
	}
	
}
